//
//  ParticleEmitter.m
//  GLSprite
//
//  Created by Daniel Polak on 7/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ParticleEmitter.h"
#import "Particle.h"


@implementation ParticleEmitter

-(void)Update:(float)fTime
{
	for (int i = 0; i < [m_vParticles nSize]; i += 1) 
	{
		//Check particle's life
		if([(Particle*)[m_vParticles objectAtIndex:i] LifeSpan] <= 0.0f)
		{
			//Remove the particle
			[[m_vParticles vector] removeObjectAtIndex: i];
		}
		
		else 
		{
			[(Particle*)[m_vParticles objectAtIndex:i] Update:fTime];
		}

	}
	
	if (!m_bComplete) 
	{
		//Create Particles
		int count = 0;
		count = (int)(m_fSpawnRate*fTime);
		
		for (int i = 0; i < count; i += 1)
		{
			float fSpan = 2.0f;
			
			//Make new particle
			CGPoint p = {m_pLocation.x, m_pLocation.y};
			Particle* _Particle = [Particle init];
			[_Particle Create:fSpan	:0 :p :p];
			
			//Add particle
			[m_vParticles push:_Particle];
		}
	}

}

-(void)Create:(int)MaxParticles:(float)SpawnRate:(CGPoint)Location
{
	m_nMaxParticles = MaxParticles;
	m_fSpawnRate = SpawnRate;
	m_bComplete = FALSE;
	m_pLocation = Location;
	[m_vParticles init];
}

-(void)Stop
{
	m_bComplete = TRUE;
}

@end
